package com.yswee.framework.widget;

import com.yswee.framework.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class TitleBar extends RelativeLayout {

	protected ImageView imgLeft;
	protected TextView tvLeft;
	protected TextView tvTitle;
	protected ImageView imgRight;
	protected TextView tvRight;

	protected Context mContext;
	
	public TitleBar(Context context) {
		super(context);
		initializeLayout(context);
	}
	
	public TitleBar(Context context, AttributeSet attrs) {
		super(context, attrs);
		initializeLayout(context);
		initializeAttr(attrs);
	}

	
	public TitleBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initializeLayout(context);
		initializeAttr(attrs);
	}
	
	protected void initializeLayout(Context context) {
		mContext = context;
		View root = LayoutInflater.from(mContext).inflate(R.layout.widget_titlebar, this, true);
		imgLeft = (ImageView) root.findViewById(R.id.titlebar_btnleft);
		tvLeft = (TextView) root.findViewById(R.id.titlebar_tvleft);
		tvTitle = (TextView) root.findViewById(R.id.titlebar_tvtitle);
		imgRight = (ImageView) root.findViewById(R.id.titlebar_btnright);
		tvRight = (TextView) root.findViewById(R.id.titlebar_tvright);
	}
	
	protected void initializeAttr(AttributeSet attrs) {
		TypedArray typedArray = getContext().obtainStyledAttributes(attrs,
				R.styleable.TitleBar);

		int lefticon = typedArray.getResourceId(R.styleable.TitleBar_TitleBar_lefticon, 0);
		setLefttIcon(lefticon);
		String lefttitle = typedArray.getString(R.styleable.TitleBar_TitleBar_lefttitle);
		setLeftTitle(lefttitle);
		String title = typedArray.getString(R.styleable.TitleBar_TitleBar_title);
		setTitle(title);
		int righticon = typedArray.getResourceId(R.styleable.TitleBar_TitleBar_righticon, 0);
		setRightIcon(righticon);
		String righttitle = typedArray.getString(R.styleable.TitleBar_TitleBar_righttitle);
		setRightTitle(righttitle);
		
		typedArray.recycle();
	}
	
	public void setLefttIcon(int resid) {
		if(resid != 0) {
			imgLeft.setImageResource(resid);
		} else {
			imgLeft.setImageResource(0);
		}
	}
	
	public void setLeftTitle(String title) {
		if(!TextUtils.isEmpty(title)) {
			tvLeft.setText(title);
			tvLeft.setVisibility(View.VISIBLE);
		} else {
			tvLeft.setVisibility(View.GONE);
		}
	}
	
	public void setTitle(String title) {
		if(!TextUtils.isEmpty(title)) {
			tvTitle.setText(title);
			tvTitle.setVisibility(View.VISIBLE);
		} else {
			tvTitle.setVisibility(View.GONE);
		}
	}
	
	public void setRightIcon(int resid) {
		if(resid != 0) {
			imgRight.setImageResource(resid);
		} else {
			imgRight.setImageResource(0);
		}
	}
	
	public void setRightTitle(String title) {
		if(!TextUtils.isEmpty(title)) {
			tvRight.setText(title);
			tvRight.setVisibility(View.VISIBLE);
		} else {
			tvRight.setVisibility(View.GONE);
		}
	}
	
	public void setLeftTitleOnClickListener(OnClickListener listener) {
		tvLeft.setOnClickListener(listener);
		imgLeft.setOnClickListener(listener);
	}
	
	public void setRightTitleOnClickListener(OnClickListener listener) {
		tvRight.setOnClickListener(listener);
		imgRight.setOnClickListener(listener);
	}
	
}
